﻿using RotovapPlatform.Domain.Business.Machine.Enums;
using RotovapPlatform.Domain.Business.Storage.Enums;
using RotovapPlatform.UI.ViewModels.MaterialManagement;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace RotovapPlatform.UI.Converters
{
	/// <summary>
	/// 将布尔值转换为字符串的转换器
	/// </summary>
	public class BooleanToStringConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is bool boolValue)
			{
				if (parameter is string paramString)
				{
					string[] options = paramString.Split('|');
					if (options.Length == 2)
					{
						return boolValue ? options[0] : options[1];
					}
				}
				return boolValue ? "是" : "否";
			}
			return string.Empty;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	public class BoolToBackgroundConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is bool isSelected && isSelected)
			{
				return new SolidColorBrush(Color.FromArgb(80, 0, 120, 215));
			}
			return new SolidColorBrush(Colors.Transparent);
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	public class BoolToGreenBrushConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return (bool)value ? Brushes.Green : Brushes.Gray;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	/// <summary>
	/// 将布尔值转换为状态指示颜色的转换器
	/// True返回绿色（表示在线/连接状态）
	/// False返回红色（表示离线/断开状态）
	/// </summary>
	public class BoolToStatusBrushConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is bool boolValue)
			{
				return boolValue ? Brushes.Green : Brushes.Red;
			}
			return Brushes.Gray;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	public class FunctionTypeToDisplayConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value == null)
				return string.Empty;

			if (value is FunctionType functionType)
			{
				// 根据表格中的映射返回对应的显示文本
				switch (functionType)
				{
					case FunctionType.Rotovap_Empty:
						return "空瓶";
					case FunctionType.Rotovap_NotEmpty:
						return "旋蒸残液";
					case FunctionType.Centrifuge_Empty:
						return "空瓶";
					case FunctionType.Centrifuge_To_Rotovap:
						return "待旋蒸液";
					case FunctionType.Centrifuge_Condensate_Collection:
						return "冷凝液";
					case FunctionType.Centrifuge_Residue_Collection:
						return "旋蒸残液";
					case FunctionType.Centrifuge_Wash_Liquid:
						return "洗液";
					case FunctionType.Sample_Empty:
						return "空瓶";
					case FunctionType.Sample_Condensate:
						return "冷凝液";
					case FunctionType.Sample_Residue:
						return "旋蒸残液";
					default:
						return functionType.ToString();
				}
			}

			return value.ToString();
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	/// <summary>
	/// 将索引转换为固定位置编号的转换器
	/// </summary>
	public class IndexConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value == null)
				return string.Empty;

			int index = 0;
			if (value is int intValue)
			{
				index = intValue;
			}
			else if (int.TryParse(value.ToString(), out int parsedValue))
			{
				index = parsedValue;
			}

			// 索引从0开始，位置编号从1开始
			index += 1;

			// 如果有层参数，则格式化为"层-位置"
			if (parameter != null)
			{
				string paramStr = parameter.ToString();

				// 处理特殊位置的标记
				if (paramStr == "SecondShelfSpecial")
				{
					// 第二层特殊位置，索引0对应2-5，索引1对应2-6
					return $"2-{index + 4}";
				}
				else if (paramStr == "ThirdShelfSpecial")
				{
					// 第三层特殊位置，索引0对应3-5，索引1对应3-6
					return $"3-{index + 4}";
				}
				else if (int.TryParse(paramStr, out int layer))
				{
					// 普通位置，直接使用层号和索引
					return $"{layer}-{index}";
				}
			}

			return index.ToString();
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	/// <summary>
	/// 将布尔值取反的转换器
	/// </summary>
	public class InverseBooleanConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is bool boolValue)
			{
				return !boolValue;
			}
			return false;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is bool boolValue)
			{
				return !boolValue;
			}
			return false;
		}
	}

	public class ItemTypeToDisplayConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value == null)
				return string.Empty;

			if (value is ExperimentConfigEnums.ItemType itemType)
			{
				// 获取枚举值的Description特性
				var fieldInfo = itemType.GetType().GetField(itemType.ToString());
				if (fieldInfo != null)
				{
					var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
					if (attributes.Length > 0)
						return attributes[0].Description;
				}

				// 如果没有Description特性，根据表格中的映射返回对应的显示文本
				switch (itemType)
				{
					case ExperimentConfigEnums.ItemType.Rotovap_500ml:
						return "500ml旋蒸瓶";
					case ExperimentConfigEnums.ItemType.Rotovap_1000ml:
						return "1000ml旋蒸瓶";
					case ExperimentConfigEnums.ItemType.CentrifugeBottle_500ml:
						return "500ml离心瓶";
					case ExperimentConfigEnums.ItemType.SampleBottle_8ml:
						return "8ml取样瓶";
					case ExperimentConfigEnums.ItemType.NormalGripper:
						return "普通夹爪";
					case ExperimentConfigEnums.ItemType.RotovapGripper:
						return "旋蒸夹爪";
					default:
						return itemType.ToString();
				}
			}

			return value.ToString();
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}


	/// <summary>
	/// 将null值转换为布尔值的转换器
	/// </summary>
	public class NullToBooleanConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return value != null;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}


	/// <summary>
	/// 将字符串转换为BottleType枚举值的转换器
	/// </summary>
	public class StringToBottleTypeConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			// 这个方向通常不需要实现，因为我们只需要从字符串转换到枚举
			return null;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is string stringValue && !string.IsNullOrEmpty(stringValue))
			{
				if (Enum.TryParse(typeof(BottleType), stringValue, out object result))
				{
					return result;
				}
			}
			return null;
		}
	}

	/// <summary>
	/// 将布尔值转换为可见性的转换器
	/// </summary>
	public class BoolToVisibilityConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is bool boolValue)
			{
				return boolValue ? Visibility.Visible : Visibility.Collapsed;
			}

			return Visibility.Collapsed;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is Visibility visibility)
			{
				return visibility == Visibility.Visible;
			}

			return false;
		}
	}


	/// <summary>
	/// 将枚举值转换为其Description特性的转换器
	/// </summary>
	public class EnumToDescriptionConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value == null)
				return string.Empty;

			// 获取枚举值
			Enum enumValue = value as Enum;
			if (enumValue == null)
				return value.ToString();

			// 获取枚举字段信息
			FieldInfo fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
			if (fieldInfo == null)
				return enumValue.ToString();

			// 获取Description特性
			DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

			// 如果找到Description特性，则返回其描述文本，否则返回枚举名称
			return attributes.Length > 0 ? attributes[0].Description : enumValue.ToString();
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			// 通常不需要从描述转换回枚举，但如果需要，可以在这里实现
			throw new NotImplementedException();
		}
	}

	/// <summary>
	/// 将布尔值取反后转换为可见性的转换器
	/// </summary>
	public class InverseBoolToVisibilityConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is bool boolValue)
			{
				return !boolValue ? Visibility.Visible : Visibility.Collapsed;
			}

			return Visibility.Visible;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is Visibility visibility)
			{
				return visibility != Visibility.Visible;
			}

			return true;
		}
	}


	/// <summary>
	/// 将布尔值转换为其反值的转换器
	/// </summary>
	public class BooleanToInvertedBooleanConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is bool boolValue)
			{
				return !boolValue;
			}

			return false;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value is bool boolValue)
			{
				return !boolValue;
			}

			return false;
		}

		/// <summary>
		/// 将null转换为Visibility的转换器
		/// 非null => Visible
		/// null => Collapsed
		/// </summary>
		public class NullVisibilityConverter : IValueConverter
		{
			public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
			{
				return value != null ? Visibility.Visible : Visibility.Collapsed;
			}

			public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 与NullVisibilityConverter相反的转换器
		/// null => Visible
		/// 非null => Collapsed
		/// </summary>
		public class InverseNullVisibilityConverter : IValueConverter
		{
			public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
			{
				return value == null ? Visibility.Visible : Visibility.Collapsed;
			}

			public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
			{
				throw new NotImplementedException();
			}
		}
	}








}
